import os
import json

import requests
import urllib

from lxml import etree

from spider.entity import Music

class CrawlUtil:
    searcher_headers={
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Cookie': 'gid=151480641267561; _xiamitoken=964805d2033cfc9d99077c67c90018f6; _unsign_token=d7b8067b47db31e5bbe5a340b0ad6f5d; user_from=1; UM_distinctid=160b17f83cea8c-0ca30691ec6086-23156660-15f900-160b17f83cfcf6; cna=jgrREmjf6QECAXPEzmCZqNX/; join_from=1zufSNtP6D010%2FjCCA; CNZZDATA921634=cnzz_eid%3D1868281538-1514802722-null%26ntime%3D1514802722; CNZZDATA2629111=cnzz_eid%3D1166285376-1514801050-null%26ntime%3D1514806450; isg=Ar29SMh7u5Z3_x_68zwXp-hW3Bl38pLIsyALZn8C-ZRDtt3oR6oBfIvkFNQL',
        'Host': 'www.xiami.com',
        'Referer': 'http://www.xiami.com/',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 OPR/49.0.2725.64'
    }

    location_headers = {
        'Accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Cookie': 'gid=151472022052912; _xiamitoken=b50ee5e7ec25b7f854e3cb765e0d310a; _unsign_token=3feec3b609dc2e8404e9aca86fc3001d; user_from=1; UM_distinctid=160ac5c5307df-06e7f9f11cffdb-23156660-15f900-160ac5c5308b34; cna=3rnPEm4y2FICAXPBzmv0w5KH; bdshare_firstime=1514720504132; __XIAMI_SESSID=2ca12e6a2a8dd9ffaf6cb1bb0efaf55c; join_from=1zufSNtP6D010%2FjCCA; CNZZDATA921634=cnzz_eid%3D1468062791-1514716321-null%26ntime%3D1514727121; CNZZDATA2629111=cnzz_eid%3D1977703969-1514720050-null%26ntime%3D1514730850; XMPLAYER_isOpen=1; __guestplay=MjA4ODY0NCw0OzIwODg2NDEsMTM7MjA4ODY0OSwxMTsxNzk4MTE5MTA2LDE7MTc5ODE1OTg1NywxOzE3OTgxNTAyNzMsMTszODg0NTQsMg%3D%3D; XMPLAYER_url=/song/playlist/id/2088644/object_name/default/object_id/0; XMPLAYER_addSongsToggler=0; isg=AkFBvcqi_9i2ABPs2Jh1JVNOQI2bxtYEl0wHkqOWGcinimFc677FMG8GGMA3',
        'Host': 'www.xiami.com',
        'Referer': 'http://www.xiami.com/play?ids=/song/playlist/id/{0}/object_name/default/object_id/0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 OPR/49.0.2725.64',
        'X-Requested-With': 'XMLHttpRequest'
    }

    def get_music(self,song_name):
        music_list=[]
        url_request='http://www.xiami.com/search?key={0}&pos=1'.format(song_name)
        res_obj=requests.get(url=url_request,headers=self.searcher_headers)
        html=etree.HTML(res_obj.text)
        music_id_list=html.xpath('//td[@class="chkbox"]/input/@value')
        music_name_list=html.xpath('//td[@class="song_name"]/a/text()')
        artist_list=html.xpath('//td[@class="song_artist"]/a[1]/text()')
        album_list=html.xpath('//td[@class="song_album"]/a/text()')
        num=len(music_id_list)
        for i in range(0,num):
            music=Music()
            music.id=self.deal_chars(music_id_list[i])
            music.name=self.deal_chars(music_name_list[i])
            music.artist=self.deal_chars(artist_list[i])
            music.album=self.deal_chars(album_list[i])
            music_list.append(music)

        return music_list

    def get_music_location(self,music_id):
        location=''
        # url_request = 'http://www.xiami.com/song/playlist/id/2088644/object_name/default/object_id/0/cat/json'
        self.location_headers['Referer'].format(music_id)
        url_request = 'http://www.xiami.com/song/playlist/id/{0}/object_name/default/object_id/0/cat/json'.format(
            music_id)
        res_obj = requests.get(url=url_request, headers=self.location_headers)
        json_str = res_obj.text.strip(' ').strip('jsonp578()')
        json_obj = json.loads(json_str)
        trackList = json_obj['data']['trackList']
        if trackList!=None:
            if 'location' in trackList[0]:
                location = trackList[0]['location']

        return location

    def get_music_path(self,location):
        path=''
        if location=='':
            print("因版权问题，该音乐无法下载")
        else:
            rows = int(location[0])
            location_length = len(location[1:])
            cols = location_length // rows
            rigth_rows = location_length % rows
            dealed_location = location[1:]
            encode_path = ""
            for i in range(0, location_length):
                mod_num = i % rows
                div_num = i / rows
                temp_num = 0
                if mod_num <= rigth_rows:
                    temp_num = mod_num * (cols + 1) + div_num
                else:
                    temp_num = rigth_rows * (cols + 1) + (mod_num - rigth_rows) * cols + div_num
                encode_path += dealed_location[int(temp_num)]
            path=urllib.parse.unquote(encode_path).replace('^', '0')

        return path

    def download_music(self,music,music_path):
        file_path=music_path+music.id+"_"+music.name+"_"+music.artist+"_"+music.album+".mp3"
        urllib.request.urlretrieve(music.path,file_path)

    def create_dir(self,music_path):
        os.mkdir(music_path)

    def get_value(self,data):
        result=''
        if len(data)>1:
            result=data[0]
        else:
            if len(data[0])==1:
                result=data
        return result

    def deal_chars(self,input_str):
        deal_str=input_str.strip(' ').replace('\r', '').replace('\n', ' ').replace('\t', '').replace('/','_')
        return deal_str






